package com.guli.eduService.controller;


import com.guli.commonUtils.R;
import com.guli.eduService.entity.EduChapter;
import com.guli.eduService.entity.vo.ChapterVO;
import com.guli.eduService.service.impl.EduChapterServiceImpl;
import com.guli.serviceBase.Entity.GuliExcepiton;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-03
 */
@Api(description = "课程章节管理")
@RestController
@RequestMapping("/eduService/chapter")
//@CrossOrigin // 解决跨域问题
public class EduChapterController {
    @Autowired
    private EduChapterServiceImpl chapterService;

    @ApiOperation(value = "获取当前课程所有章节信息")
    @GetMapping("/listChapter/{courseId}")
    public R getChapterData(
            @ApiParam(name = "courseId" , value = "课程id" , required = true)
            @PathVariable String courseId){
        // 获取当前课程的所有章节/小节信息;
        List<ChapterVO> result = chapterService.listChapterData(courseId);
        return R.ok().data("ChapterData" , result);
    }

    @ApiOperation(value = "添加章节信息")
    @PostMapping("addChapter")
    public R addChapter(
            @ApiParam(name = "chapter" , value = "章节信息" ,required = true)
            @RequestBody EduChapter chapter){
        // 特殊情况 ：即，当前所要添加的章节信息id，已经存在于数据库中;
        // （1）虽然考录到此种特殊情况，但是添加章节的id是实体类中自增的，所以必然是不可能出现重复
        // （2）前端页面，所要传递的数据中并不会包含所添加章节的id值;
        EduChapter getChapter = chapterService.getById(chapter.getId());
        if(getChapter != null){
            throw new GuliExcepiton(20006 , "当前章节信息已经存在，无法添加！！！");
        }
        boolean flag = chapterService.save(chapter);
        if(flag){
            return R.ok().message("添加章节成功~~~");
        }
        else {
            throw new GuliExcepiton(20001 , "添加章节失败！！！");
        }
    }

    @ApiOperation(value = "修改章节信息")
    @PostMapping("updateChapter")
    public R updateChapter(
            @ApiParam(name = "chapter" , value = "章节信息" ,required = true)
            @RequestBody EduChapter chapter){
        boolean flag = chapterService.updateById(chapter);
        if(flag){
            return R.ok().message("修改章节成功~~~");
        }
        else {
            throw new GuliExcepiton(20002 , "修改章节失败！！！");
        }
    }

    @ApiOperation(value = "根据id，查询章节信息")
    @GetMapping("queryChapter/{chapterId}")
    public R getChapterById(
            @ApiParam(name = "chapterId" , value = "章节id" , required = true)
            @PathVariable String chapterId){
        EduChapter chapterInfo = chapterService.getById(chapterId);
        if(chapterInfo == null){
            throw new GuliExcepiton(20003 , "所查询的章节信息不存在！！！");
        }
        else {
            return R.ok().data("chapterInfo" , chapterInfo);
        }
    }

    @ApiOperation(value = "删除指定id的章节信息")
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapterById(
            @ApiParam(name = "chapterId" , value = "章节id" , required = true)
            @PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok().message("删除章节成功~~~");
        }
        else {
            throw new GuliExcepiton(20004 , "删除章节失败！！！");
        }
    }
}

